package com.xpoint.backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xpoint.backend.common.R;
import com.xpoint.backend.entity.Category;
import com.xpoint.backend.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
//分类管理
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Cacheable(value = "category_page",key = "'category_'+#page+'_'+#pageSize")
    @GetMapping("/page")
//    分页查询
    public R<Page> findPage(Long page, Long pageSize){
        return categoryService.fnidPageByCategory(page,pageSize);
    }

    @CacheEvict(value = {"category","category_page","SetmealDtoByPage","dish"},allEntries = true)
    @DeleteMapping
//    删除分类
    public R<String> deleteCategory(Long id){
        return categoryService.removeCategoryById(id);
    }

    @CacheEvict(value = {"category","category_page"},allEntries = true)
    @PostMapping
//    添加分类
    public R<String> addCategory(@RequestBody Category category){
        return categoryService.addCategory(category);
    }

    @CacheEvict(value = {"category","category_page"},allEntries = true)
    @PutMapping
//    修改分类
    public R<String> updateCategory(@RequestBody Category category){
        return categoryService.updateCategory(category);
    }

    @Cacheable(value = "category",key = "#type+'_'+'category'")
    @GetMapping("/list")
//    查询菜品or套餐分类
    public R<List<Category>> findType(Long type){
        return categoryService.findDishTypeList(type);
    }
}
